import Taro from '@tarojs/taro'
import {View,Picker} from '@tarojs/components'
import {AtList, AtListItem,AtGrid} from "taro-ui"
import UserProfile from '../../components/UserProfile/index'
import {navigateWithAuth, redirectWithAuth, toast} from "../../utils";
import './index.scss'
import {getUserById} from "../../service";
import {updateUserLastTimeClass} from './service'
import {getGlobalData} from "../../global.d";

export default class Index extends Taro.Component {

  constructor(props) {
    super(props);
    this.doActionWithMiddleClick = this.doActionWithMiddleClick.bind(this);
    this.state = {
      user:'',
      classList:[],
    }
  }

  componentDidMount() {
    Taro.setNavigationBarTitle({
      title:'我的'
    })
    this.setClassList()
  }

  setClassList(){
    let user = getGlobalData('user')
    let list = []
    user.userClass.map((item,index)=>{
      list.push(item.className)
    })
    this.setState({
      user,
      classList: list
    })
    if(!user.userDetail.userRealName){
      toast('请完善你的个人信息')
      Taro.navigateTo({
        url:'/pages/my/personInfo/index'
      })
    }
  }

  doActionWithMiddleClick(item,index){
    switch (index) {
      case 0:

        break;
      case 1:
        navigateWithAuth('/pages/my/manageStudents/index')
        break;
      case 2:

        break;
    }
  }

  onChange = async e => {
    let userNowClass = this.state.user.userClass[e.detail.value]
    this.setState({
      userNowClass
    })
    console.log(userNowClass)
    await updateUserLastTimeClass(this.state.user._id, userNowClass)
    redirectWithAuth('/pages/guide/index')
  };
  render() {
    const {user} = this.state;
    const middleToolList = [
      {
        iconInfo:{ size: 25, prefixClass:'icon', value: 'yaoqing' },
        value: '邀请成员'
      },{
        iconInfo:{ size: 25, prefixClass:'icon', value: 'qunchengyuanguanlibtn' },
        value: '成员管理'
      },{
        iconInfo:{ size: 25, prefixClass:'icon', value: 'qiehuan1' },
        value: '切换班级'
      }

    ]
    return (
      <View className='my'>

        <UserProfile user={user} />
        <View className='my_middle' >
          <AtGrid
            data={middleToolList}
            className='my_middle_tools'
            onClick={this.doActionWithMiddleClick}
          />
          <Picker
          mode='selector'
          onChange={this.onChange}
          range={this.state.classList}
           >
          <View  className='my_middle_picker'>

          </View>
        </Picker>
        </View>
        <AtList>
          <AtListItem title='个人信息' arrow='right' iconInfo={{prefixClass:'icon', value: '-gerenxinxi', size: 20}}  onClick={() => navigateWithAuth('/pages/my/personInfo/index')} />
          <AtListItem title='加入班级' arrow='right' iconInfo={{prefixClass:'icon',value: 'jiaruqun', size: 20}} onClick={() => navigateWithAuth('/pages/my/joinClass/index')} />
          <AtListItem title='创建班级' arrow='right' iconInfo={{prefixClass:'icon',value: 'chuangjianyingyong', size: 20}} onClick={() => navigateWithAuth('/pages/my/createNewClass/index')} />
          <AtListItem title='管理班级' arrow='right' iconInfo={{prefixClass:'icon',value: 'guanli01-copy', size: 20}} onClick={() => navigateWithAuth('/pages/my/manageClasses/index')} />
          <AtListItem title='我要反馈' arrow='right' iconInfo={{prefixClass:'icon',value: 'fankui', size: 20,color:null}} onClick={() => navigateWithAuth('/pages/my/feedBack/index')} />

        </AtList>
      </View>
    );
  }
}
